<!--   [PAGE]] : 个人项目模板
       @Author  : ljq
       @Created : 2025/10/8
 -->
<template>
  <div class="app-container">
    <!-- ------------------------ START: 查询框 -------------------------->
    <el-form class='query' :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">

      <!--      <el-form-item>-->
      <!--        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
      <!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
      <!--      </el-form-item>-->
      <!--    -->
    </el-form>
    <!-- ------------------------- END: 查询框 --------------------------->

    <!-- ---------------------- START: 主数据区域------------------------->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-back" size="mini" @click="toBack">返回</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="[`${permi}:add`]">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="[`${permi}:remove`]">批量删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"/>
    </el-row>


    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="编号" align="center" prop="id" v-if="columns[0].visible"/>
      <el-table-column label="名称" align="center" prop="comment" v-if="columns[3].visible"/>
      <el-table-column label="上班时间区间" align="center" prop="planWorkTimeRange" v-if="columns[1].visible"/>
      <el-table-column label="计划工时" align="center" prop="planHour" v-if="columns[2].visible"/>
      <el-table-column label="审批人" align="center" prop="approvalBy" v-if="columns[4].visible">
        <template slot-scope="scope">
          <select-user isCol :value="scope.row.approvalBy"/>
        </template>
      </el-table-column>
      <el-table-column prop="status" align="center" label="状态" v-if="columns[5].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[6].visible"/>
      <el-table-column label="创建者" align="center" prop="createBy" v-if="columns[7].visible"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" v-if="columns[8].visible"/>
      <el-table-column label="更新者" align="center" prop="updateBy" v-if="columns[9].visible"/>

      <!-- 操作列组件 -->
      <operation-column :permi="permi" @edit="handleUpdateOver" @delete="handleDelete"/>


    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
    <!-- ---------------------- END: 主数据区域 ------------------------>

    <!-- ------------------------ START: 弹出框 -------------------------->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="comment">
              <el-input v-model="form.comment" placeholder="请输入名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="状态" filterable clearable class="mb8">
                <el-option v-for="dict in dict.type.status" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="上班时间">
            <el-time-select v-model="tempWorkTimeRange1.start" placeholder="选择时间范围"
                            :picker-options="{
                                start: '05:00',
                                step: '00:15',
                                end: '23:00'
                              }" class="time-select"
                            @change="updateWorkTimeRange"/>
            -
            <el-time-select v-model="tempWorkTimeRange1.end" placeholder="选择时间范围"
                            :picker-options="{
                                start: '05:00',
                                step: '00:15',
                                end: '23:00'
                              }" class="time-select"
                            @change="updateWorkTimeRange"/>
            <span style="margin-left: 5rem"></span>
            <el-time-select v-model="tempWorkTimeRange2.start" placeholder="选择时间范围"
                            :picker-options="{
                                start: '05:00',
                                step: '00:15',
                                end: '23:00'
                              }" class="time-select"
                            @change="updateWorkTimeRange"/>
            -
            <el-time-select v-model="tempWorkTimeRange2.end" placeholder="选择时间范围"
                            :picker-options="{
                                start: '05:00',
                                step: '00:15',
                                end: '23:00'
                              }" class="time-select"
                            @change="updateWorkTimeRange"/>
            </el-form-item>
            <el-form-item label="上班时间" prop="planWorkTimeRange" v-show="false">
              <el-input v-model="form.planWorkTimeRange" placeholder="请输入上班时间区间"/>
            </el-form-item>

          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划工时" prop="planHour">
              <el-input v-model="form.planHour" placeholder="请输入计划工时"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="审批人" prop="approvalBy">
              <select-user v-model="form.approvalBy" placeholder="请选择审批人" multiple/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="时薪" prop="hourlyWage">
              <el-input v-model="form.hourlyWage" placeholder="请输入时薪"/>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- ------------------------ END: 弹出框 -------------------------->
  </div>
</template>
<script>
import {listProjectDetailTemplate, getProjectDetailTemplate, addProjectDetailTemplate, updateProjectDetailTemplate, delProjectDetailTemplate} from "@/api/business/projectDetailTemplate";
import {initData, baseMethods} from "@/utils/basePage";

let baseData = initData();
export default {
  name: "ProjectTemplate",
  dicts: ['status'],
  data() {
    return {
      ...baseData,
      permi: 'business:projectDetailTemplate',
      // 列信息
      columns: [
        {key: 0, label: `编号`, visible: false},
        {key: 1, label: `上班时间区间`, visible: true},
        {key: 2, label: `计划工时`, visible: true},
        {key: 3, label: `名称`, visible: true},
        {key: 4, label: `审批人`, visible: true},
        {key: 5, label: `状态`, visible: true},
        {key: 6, label: `创建时间`, visible: false},
        {key: 7, label: `创建者`, visible: false},
        {key: 8, label: `更新时间`, visible: false},
        {key: 9, label: `创建者`, visible: false},
      ],
      // 表单校验
      rules: {
        planWorkTimeRange: [{required: true, message: "上班时间区间不能为空", trigger: "blur"}],
        planHour: [{required: true, message: "计划工时不能为空", trigger: "blur"}],
        comment: [{required: true, message: "名称不能为空", trigger: "blur"}],
        // approvalBy: [{required: true, message: "审批人不能为空", trigger: "blur"}],
        hourlyWage: [{required: true, message: "时薪不能为空", trigger: "blur"}],
      },

      tempWorkTimeRange1: {start: '', end: ''},
      tempWorkTimeRange2: {start: '', end: ''},


    };
  },
  created() {
    // 接收query参数
    let params = this.$route.query;
    this.defaultForm.projectId = params.projectId;
    this.defaultForm.type = '1';
    this.defaultForm.status = '1';

    this.queryParams.projectId = params.projectId;
    this.queryParams.type = '1';
    this.getList();

  },
  methods: {
    ...baseMethods(listProjectDetailTemplate, getProjectDetailTemplate, addProjectDetailTemplate, updateProjectDetailTemplate, delProjectDetailTemplate),
    toBack() {
      this.$router.back()
    },

    handleUpdateOver(row) {
      this.handleUpdate(row);
      let [range1, range2] = row.planWorkTimeRange.split(",");
      if (range1) {
        let temp1 = range1.split("-").map(xx => xx.trim());
        this.tempWorkTimeRange1.start = temp1[0]
        this.tempWorkTimeRange1.end = temp1[1]
      }
      if (range2) {
        let temp2 = range2.split("-").map(xx => xx.trim());
        this.tempWorkTimeRange2.start = temp2[0]
        this.tempWorkTimeRange2.end = temp2[1]
      }
    },

    updateWorkTimeRange() {
      let result = '';
      {
        const {start, end} = this.tempWorkTimeRange1;
        result += `${start} - ${end}`
      }
      {
        const {start, end} = this.tempWorkTimeRange2;
        result += ` , ${start} - ${end}`
      }
      this.form.planWorkTimeRange = result;

    }
  }
}
;
</script>

<style scoped>
.time-select {
  width: 7.5rem !important;
  margin: 0 5px;
  font-size: 13px
}
</style>
